<?php

namespace Sys;

use C\L\AdmController;

class SigninController extends AdmController
{
    protected function init()
    {
        $this->service = $this->s_syssignin;

        $this->pubSearchKeys = [
        'type', 'day'
        ];

        $this->hideKeys = [
        'is_delete'
        ];

        $this->createKeys = [
        'type', 'reward_type', 'reward_num', 'day', 'days', 'coupon'
        ];

        $this->updateKeys = [
        'type', 'reward_type', 'reward_num', 'day', 'days', 'coupon'
        ];

        $this->timeToDateKeys = [
        'uptime', 'addtime'
        ];
    }

    protected function beforeCreate(&$data)
    {
        $this->rule($data);
        $this->couponAdd($data);

        return true;
    }

    protected function beforeUpdate(&$data)
    {
        $id = $this->getValue('id');
        $this->rule($data, $id);
        $this->couponAdd($data);

        return true;
    }

    private function rule(&$data, $id = '')
    {
        if (isset($data['coupon']) and $data['coupon'] != '') {
            $data['coupon'] = json_decode($data['coupon'], true);
        }
        if (!in_array($data['type'], [1, 2])) {
            $this->error('参数错误');
        }

        if (!in_array($data['reward_type'], [1, 2])) {
            $this->error('奖励类型错误');
        }

        if ($data['type'] == 1) {
            $date = date('Ymd');

            if (!isset($data['day']) or $data['day'] < $date) {
                $this->error('日期设置错误');
            }

            $where = ['type' => $data['type'], 'day' => $data['day']];
        } else {
            $this_month = date('Ym');
          // if(!isset($data['day']) or strlen($data['day'])!=6 or $data['day'] <= $this_month){
            if (!isset($data['day']) or strlen($data['day']) != 6) {
                $this->error('月份设置错误');
            }
            $month_days = date('t', $data['day']);
            if (!isset($data['days']) or $data['days'] < 2 or $data['days'] > $month_days) {
                $this->error('累计天数错误');
            }

            $where = ['type' => $data['type'], 'day' => $data['day'], 'days' => $data['days']];
        }

        $old = $this->di['s_syssignin']->search($where);

        if ($old) {
            if ($id == '') {
                $this->error('不可重复设置');
            } else {
                if ($old['id'] != $id) {
                    $this->error('不可重复设置');
                }
            }
        }

        if ($data['reward_type'] == 2) {
            if ($data['coupon']['coupon_value'] == 0 or $data['coupon']['effective_time'] == 0 or $data['coupon']['use_condition'] == 0) {
                $this->error('代金券参数错误');
            }
            if ($data['coupon']['coupon_value'] >= $data['coupon']['use_condition']) {
                $this->error('代金券参数错误');
            }
        }
    }

    private function couponAdd(&$data)
    {
        if ($data['reward_type'] == 2) {
            $coupon = $data['coupon'];
            $data['reward_num'] = $this->s_coupon->createVoucher($coupon);
            unset($data['coupon']);
        }
        return true;
    }

    protected function beforeSearch()
    {
        $type = $this->getValue('type', true);
        $this->params['order'] = 'day asc';

        if ($type == 1) {
            $stime = $this->getValue('sdate', true);
            $etime = $this->getValue('edate', true);

            $this->params['data']['day'] = [$stime, $etime];
            $this->params['data_type']['day'] = 'between';
        }

        return true;
    }

    protected function afterSearch(&$data)
    {
        foreach ($data['list'] as $k => &$v) {
            if ($v['reward_type'] == 2) {
                $v['coupon'] = $this->di['s_coupon']->search(['id' => $v['reward_num']], [], ['id', 'coupon_value', 'use_condition', 'effective_time']);
            }
        }
        return true;
    }

    public function detailAction()
    {
        $id = $this->getValue('id', true);
        $res = $this->service->getDetail(['id' => $id]);
        $this->success($res);
    }

    public function rowAction()
    {
        $type = $this->getValue('type', true);
        $where = ' type=' . $type;
        if ($type == 1) {
            $stime = $this->getValue('sdate', true);
            $etime = $this->getValue('edate', true);

            $where .= ' and day between ' . $stime . ' and ' . $etime;
            $order = ' day asc';
          // echo $where;exit;

            $res = $this->s_syssignin->getContinuityList($where, $order);
        } elseif ($type == 2) {
            $day = $this->getValue('day', true);

            $where .= ' and day=' . $day;
            $order = ' days asc';
            $res = $this->s_syssignin->getContinuityList($where, $order);
        } else {
            $this->error('参数错误');
        }
        if ($res) {
            $this->success($res);
        }
        $this->success([]);
    }
}
